package net.adst.ui.mainframe.toppanel;

import net.adst.beans.User;
import net.adst.resources.VariableNames;
import net.adst.ui.other.AddNaemItemDialog;
import net.adst.ui.other.EditNaemItemDialog;
import net.adst.ui.other.EditPasswordDialog;
import net.adst.ui.other.EditUsersDialog;
import net.adst.util.DataManager;
import net.adst.util.FileHelper;
import net.adst.util.GBC;
import net.adst.util.fileFilters.MSExcelFilter;
import sun.net.ResourceManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 02.11.12
 * Time: 1:50
 * To change this template use File | Settings | File Templates.
 */
public class ButtonsArea extends JPanel {

    private JButton userButton;
    private JButton addRowButton;
    private JButton editRowButton;
    private JButton exlButton;
    private User user;

    public ButtonsArea() {
        setLayout(new GridBagLayout());
        this.user = (User) DataManager.getObject(VariableNames.USER);
        addButtons();
    }

    private void addButtons() {
        userButton = new JButton(user.isAdmin() ? "работа с кадрами" : "изменить пароль");
        userButton.setFocusPainted(false);
        userButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (user.isAdmin()) {
                    EditUsersDialog dialog = new EditUsersDialog(user);
                    dialog.init();
                } else {
                    EditPasswordDialog dialog = new EditPasswordDialog(user);
                    dialog.init();
                }

            }
        });
        addRowButton = new JButton("доб запись");
        addRowButton.setFocusPainted(false);
        addRowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AddNaemItemDialog dialog = new AddNaemItemDialog();
                dialog.init();
            }
        });
        editRowButton = new JButton("Редактировать запись");
        editRowButton.setFocusPainted(false);
        editRowButton.setEnabled(false);
        DataManager.setObject(VariableNames.UI_TOP_PANEL_EDIT_BUTTON, editRowButton);
        editRowButton.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        EditNaemItemDialog dialog = new EditNaemItemDialog();
                        dialog.init();
                    }
                });
        exlButton = new JButton(new ImageIcon(ResourceManager.class.getResource("/images/excel.png")));
        exlButton.setFocusPainted(false);
        exlButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveNewExcelAction();
            }
        });
        add(userButton, new GBC(0, 0).setInsets(40, 0));
        add(addRowButton, new GBC(1, 0).setInsets(40, 10));
        add(editRowButton, new GBC(2, 0).setInsets(0, 7));
        add(exlButton, new GBC(3, 0).setInsets(0, 7));
    }


    public static void saveNewExcelAction() {
        final JFileChooser fileChooser = new JFileChooser();
        MSExcelFilter msExcelFilter = new MSExcelFilter();
        fileChooser.addChoosableFileFilter(msExcelFilter);
        fileChooser.setFileFilter(msExcelFilter);
        int rc = fileChooser.showSaveDialog(null);
        int response;
        while (rc == JFileChooser.APPROVE_OPTION && !FileHelper.verifyFileName(fileChooser.getCurrentDirectory(), fileChooser.getSelectedFile())) {
            response = JOptionPane.showOptionDialog(null, "Файл " + fileChooser.getSelectedFile() + " Уже существует. Хотите заменить его? ", "Сохранить", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null);
            if (response == JOptionPane.NO_OPTION) {
                rc = fileChooser.showSaveDialog(null);
            }
            if (response == JOptionPane.YES_OPTION) {
                rc = JFileChooser.CANCEL_OPTION;
            }
        }
        File file = fileChooser.getSelectedFile();
        if (file != null) {
            fileChooser.getComponentListeners();
            FileHelper.createResultExcelFile(file);
        } else {
            return;
        }

    }
}
